<?php
/*
+--------------------------------------------------------------------------
|   WeCenter [#RELEASE_VERSION#]
|   ========================================
|   by WeCenter Software
|   © 2011 - 2014 WeCenter. All Rights Reserved
|   http://www.wecenter.com
|   ========================================
|   Support: WeCenter@qq.com
|
+---------------------------------------------------------------------------
*/

if (! defined('IN_ANWSION'))
{
	die();
}

class advtise extends AWS_ADMIN_CONTROLLER
{
	public function setup()
	{
		TPL::assign('menu_list', $this->model('admin')->fetch_menu_list(666));
	}

	public function list_action()
	{     
            //var_dump($_POST);die;
            if ($this->is_post())
		{
               
			foreach ($_POST as $key => $val)
			{
				if ($key == 'keyword' )
				{
                                    //rawurlencode（）保护原义字符以免其被解释为特殊的 URL 定界符
					$val = rawurlencode($val);
				}

				$param[] = $key . '-' . $val;
			}

			H::ajax_json_output(AWS_APP::RSM(array(
				'url' => get_js_url('/admin/advtise/list/' . implode('__', $param))
			), 1, null));
		}
                  $where = array(
                'is_use'=>1,
            );  
             
            if ($_GET['keyword'])
		{       //quote()方法是防止添加引号防止数据库攻击
			$where[] = "(`title` LIKE '%" . $this->model('advtise')->quote($_GET['keyword']) . "%')";
		}
               // var_dump($where);die;
                
           if($adv_list = $this->model('advtise')->fetch_page('advertise', implode(' AND ', $where), 'id DESC', $_GET['page'], $this->per_page))
            {
			//$search_articles_total = $this->model('advtise')->found_rows();
                        $search_articles_total = $this->model('advtise')->count('advertise',implode(' AND ', $where));
            }
            //var_dump($search_articles_total);die;
            if($adv_list){
                         foreach ($adv_list AS $key => $val)
			{
				$articles_list_uids[$val['bind_id']] = $val['bind_id'];
			}
                        if ($articles_list_uids)
			{
				$articles_list_user_infos = $this->model('article')->get_article_info_by_ids($articles_list_uids);
			}
                        foreach ($adv_list AS $key => $val)
			{
				$adv_list[$key]['bind_article'] = $articles_list_user_infos[$val['bind_id']];
			}
                        
            }     
            //var_dump($adv_list);die;
            TPL::assign('pagination', AWS_APP::pagination()->initialize(array(
			'base_url' => get_js_url('/admin/advtise/list/') . implode('__', $url_param),
			'total_rows' => $search_articles_total,
			'per_page' => $this->per_page
		))->create_links());

		$this->crumb(AWS_APP::lang()->_t('广告管理'), 'admin/advtise/list/');

		TPL::assign('articles_count', $search_articles_total);
		TPL::assign('list', $adv_list);
            
		TPL::output('admin/advtise/list');
	}
        
        public  function add_action(){
           // $uid = $_GET['uid'];
           // var_dump($uid);die;
            //$url = $_SERVER["QUERY_STRING"];
            //$ids = explode('-', $url);
           // if(isset($ids[1])){
            if(isset($_GET['uid'])){
                $advertise_info = $this->model('advtise')->get_advtise_info_by_id($_GET['uid']);                
                TPL::assign('advertise_info', $advertise_info);
                 TPL::output('admin/advtise/edit');
            }
             TPL::output('admin/advtise/add');
        }
      
        
        
}